﻿#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    ItemBaseEditorDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    table = TD_PASSWD;
    itemModel=new QStandardItemModel(this);
    ui->treeView->setModel(itemModel);
    ui->treeView->setEditTriggers(QTreeView::NoEditTriggers);
    updateView();
    createMenu();
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::updateView()
{
    updateView_Two();
    ui->treeView->resizeColumnToContents(0);
    ui->status->clear();
}

void Dialog::generateMenu()
{
    menu->addAction(QStringLiteral("添加..."),this,&Dialog::newItem);
    menu->addAction(QStringLiteral("搜索 (F2)"),this,&Dialog::search);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新"),this,&Dialog::updateView);
    menu->addAction(QStringLiteral("全部折叠"),this,&Dialog::collapseAll);
    menu->addSeparator();
    menu->addAction(QStringLiteral("打开编辑器"),this,&Dialog::editItem);
}

void Dialog::collapseAll()
{
    ui->treeView->collapseAll();
}

void Dialog::newItem()
{
    ItemAdd itemAdd(this,table);

    QModelIndex index=ui->treeView->currentIndex();
    if(getCategoryByIndex(index))
        itemAdd.setCategory(category);

    if(itemAdd.exec() == QDialog::Accepted)
        updateView();
}

void Dialog::editItem()
{
    ItemEdit edit(this,table);
    edit.move(this->x() + 300,this->y());

    if(edit.exec() == QDialog::Accepted)
        updateView();
}

void Dialog::on_treeView_clicked(const QModelIndex &index)
{
    if(!getItemByIndex(index))
        return;

    if(getDetail())
        ui->status->setText(QStringLiteral("密码：") + detail);
}
